package com.banciyuan.bcywebview.biz.post;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;

import de.greenrobot.daoexample.model.Rules;

/**
 * Created by hy on 2015/12/30.
 */
public class RulesDialog extends Dialog {
    public RulesDialog(Context context) {
        super(context);
    }

    public RulesDialog(Context context, int theme) {
        super(context, theme);
    }

    protected RulesDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder implements View.OnClickListener {
        private Rules rules;
        private Context context;
        private TextView tvRuleTile, tvRuleContent, tvRuleConfirm;
        private View btnConfirm;
        private ImageView ivConfirm;
        private OnClickListener confirmListener;
        private RulesDialog mydialog;
        private boolean showBlackBg;

        public Builder(Context context, Rules rules) {
            this.context = context;
            this.rules = rules;
        }

        public Builder(Context context, Rules rules, boolean showBlackBg) {
            this.context = context;
            this.rules = rules;
            this.showBlackBg = showBlackBg;
        }

        public Builder setTitle(String title) {
            return this;
        }


        public RulesDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (showBlackBg) {
                mydialog = new RulesDialog(context, R.style.Dialog);
            } else {
                mydialog = new RulesDialog(context, R.style.BestDialog);
            }
            View layout = inflater.inflate(R.layout.rule_dialog, null);

            tvRuleTile = (TextView) layout.findViewById(R.id.tv_rule_title);
            tvRuleContent = (TextView) layout.findViewById(R.id.tv_rule_content);
            tvRuleConfirm = (TextView) layout.findViewById(R.id.tv_rule_confirm);
            tvRuleTile.setText(Html.fromHtml(rules.getTitle()));
            tvRuleContent.setText(Html.fromHtml(rules.getContent()));
            tvRuleConfirm.setText(Html.fromHtml(rules.getConfirm()));

            ivConfirm = (ImageView) layout.findViewById(R.id.iv_confirm);
            btnConfirm = layout.findViewById(R.id.btn_confirm);
            mydialog.setContentView(layout);
            ivConfirm.setTag(false);
            layout.findViewById(R.id.iv_close).setOnClickListener(this);
            layout.findViewById(R.id.tv_rule_confirm).setOnClickListener(this);
            ivConfirm.setOnClickListener(this);
            btnConfirm.setOnClickListener(this);
            return mydialog;
        }

        public Builder setConfirmButton(DialogInterface.OnClickListener listener) {
            this.confirmListener = listener;
            return this;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_rule_confirm:
                case R.id.iv_confirm:
                    if ((boolean) ivConfirm.getTag()) {
                        ivConfirm.setImageResource(R.drawable.shape_rect_gray);
                        btnConfirm.setBackgroundResource(R.drawable.shape_timeline_gray_bigtwo);
                        ivConfirm.setTag(false);
                    } else {
                        ivConfirm.setImageResource(R.drawable.checked_pink);
                        btnConfirm.setBackgroundResource(R.drawable.shape_timeline_pink_bigtwo);
                        ivConfirm.setTag(true);
                    }
                    break;
                case R.id.btn_confirm:
                    if ((boolean) ivConfirm.getTag())
                        if (null != confirmListener)
                            confirmListener.onClick(mydialog, DialogInterface.BUTTON_POSITIVE);
                    break;
                case R.id.iv_close:
                    mydialog.dismiss();
                    break;
            }
        }
    }
}
